package vn.bus.bk.adapter;

import java.util.ArrayList;

import vn.bus.bk.R;
import vn.bus.bk.object.ItemObject;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class GridMenuAdapter extends BaseAdapter {

    private ArrayList<ItemObject> arrayListItem;
    protected int width, height;
    private LayoutInflater inflater;

    public GridMenuAdapter(Activity activity, ArrayList<ItemObject> array,
	    int width, int height) {
	this.arrayListItem = array;
	inflater = (LayoutInflater) activity
		.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	this.width = width;
	this.height = height;
    }

    public int getCount() {
	// TODO Auto-generated method stub
	return arrayListItem.size();
    }

    public Object getItem(int arg0) {
	// TODO Auto-generated method stub
	return arg0;
    }

    public long getItemId(int arg0) {
	// TODO Auto-generated method stub
	return arg0;
    }

    public View getView(int position, View convertView, ViewGroup arg2) {
	// TODO Auto-generated method stubn
	Holder holder = new Holder();
	if (convertView == null) {
	    convertView = inflater.inflate(R.layout.item_layout, null);

	    holder.image = (ImageView) convertView
		    .findViewById(R.id.imgIconItem);
	    holder.description = (TextView) convertView
		    .findViewById(R.id.tvDescriptionItem);
	    holder.layout = (RelativeLayout) convertView
		    .findViewById(R.id.layout_grid);
	    int ngang = 20*height/800;
	    int doc = 20*width/480;
	    holder.layout.setPadding(doc, ngang, doc, ngang);
	    
	    holder.image.getLayoutParams().width = 120*width/480;
	    holder.image.getLayoutParams().height = 120*height/800;
	    
	    convertView.setTag(holder);
	}

	ItemObject i = arrayListItem.get(position);
	if (i != null) {
	    holder = (Holder) convertView.getTag();
	    holder.image.setImageBitmap(i.getBitmap());
	    holder.description.setText(i.getDescription());

	}
	return convertView;
    }

}

class Holder {
    ImageView image;
    TextView description;
    RelativeLayout layout;

}
